package tt.dz.egobus.servicemanager.core.resovler;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

/**
 * @author Anson
 * @date 17-3-16
 * @name 各种转换值
 * @since 1.0.1
 */
public enum ClassPrototypeEnum implements Converter<String, Object> {

    INT {
        @Override
        public Integer convert(String source) {
            return NumberUtils.isDigits(source) ? NumberUtils.toInt(source) : 0;
        }
    },
    INTEGER {
        @Override
        public Integer convert(String source) {
            return NumberUtils.isDigits(source) ? NumberUtils.toInt(source) : 0;
        }
    },
    BOOLEAN {
        @Override
        public Boolean convert(String source) {
            return BooleanUtils.toBoolean(source);
        }
    },
    SHORT {
        @Override
        public Short convert(String source) {
            return NumberUtils.isDigits(source) ? NumberUtils.toShort(source) : (short) 0;
        }
    },
    LONG {
        @Override
        public Long convert(String source) {
            return NumberUtils.isDigits(source) ? NumberUtils.toLong(source) : (long) 0;
        }
    },
    DOUBLE {
        @Override
        public Double convert(String source) {
            return NumberUtils.isNumber(source) ? NumberUtils.toDouble(source) : (double) 0;
        }
    },
    FLOAT {
        @Override
        public Float convert(String source) {
            return NumberUtils.isNumber(source) ? NumberUtils.toFloat(source) : (float) 0;
        }
    },
    BYTE {
        @Override
        public Byte convert(String source) {
            return NumberUtils.isNumber(source) ? NumberUtils.toByte(source) : (byte) 0;
        }
    },
    DATE {
        @Override
        public Date convert(String source) {
            try {
                return DateUtils.parseDate(source, tt.dz.egobus.servicemanager.util.DateUtils.toDateFormate());
            } catch (ParseException e) {
                return null;
            }
        }
    },
    ENDDATE {

        private String HMS = "23:59:59";

        /**
         *
         * @param source 转换 时间的参数 类型的数据，并在尾部添加当天最后一次时间戳
         * @author Anson
         * @date 17-4-13
         * @name
         * @since 1.0.4
         *
         */
        @Override
        public Date convert(String source) {
            try {
                Date date = (Date) DATE.convert(source);
                if (date == null) {
                    return date;
                }
                String fmt = DateFormatUtils.format(date,
                        tt.dz.egobus.servicemanager.util.DateUtils.DATE_TO_STRING_SHORT_PATTERN);
                fmt += " " + HMS;
                return DateUtils.parseDate(fmt, tt.dz.egobus.servicemanager.util.DateUtils.toDateFormate());
            } catch (ParseException e) {
                return null;
            }
        }
    }, STRING {
        @Override
        public String convert(String source) {
            return source;
        }
    },;


    @Override
    public abstract Object convert(String source);


    public <T> T value(String source, Class<T> clazz) {
        return (T) this.convert(source);
    }

    public static final ClassPrototypeEnum valueOfKey(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return ClassPrototypeEnum.valueOf(key.trim().toUpperCase());
    }


}